﻿using LibraryMangementSystem.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookManage
{
    public partial class PublisherInfoForm : Form
    {
        public PublisherInfoForm()
        {
            InitializeComponent();
        }

        private void data_loca(object sender, EventArgs e)
        {
            LibraryEntities1 dbContext = new LibraryEntities1();
            /* IList<Publisherlnfo> list = dbContext.Publisherlnfo.SqlQuery("select PublisherId,Publisher from Publisherlnfo").ToList();
             dataGridView1.DataSource = list;*/
            List<Publisherlnfo> publisherinfo = dbContext.Publisherlnfo.ToList<Publisherlnfo>();
            List<ResultPublish> list = new List<ResultPublish>();
            foreach (Publisherlnfo p in publisherinfo)
            {
                ResultPublish resultPublish = new ResultPublish();
                resultPublish.PublisherId = p.PublisherId.Trim();
                resultPublish.Publisher = p.Publisher.Trim();
                resultPublish.Address = p.Address.Trim();
                resultPublish.Tel = p.Tel.Trim();
                resultPublish.Fax = p.Fax.Trim();
                resultPublish.Postcode = p.Postcode;
                resultPublish.http = p.http.Trim();
                list.Add(resultPublish);

            }
            /*publisherinfo.Remove()*/
            dataGridView1.DataSource = list;
        }

        private void SelectRow(object sender, EventArgs e)
        {


        }

        private void button_update(object sender, EventArgs e)
        {
            int n = dataGridView1.SelectedRows.Count;//获取当前选中的行
            /*
            for (int i=0;i<n;i++) {
                MessageBox.Show(dataGridView1.SelectedRows[i].Cells[1].Value.ToString());
            }*/
            if (n != 1)
            {
                MessageBox.Show("请选择一行！");
                return;
            }
            string k = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            if (k != null && k != "")
            {
                PublishUpdate p = new PublishUpdate(k);
                p.ShowDialog();
                if (p.DialogResult == DialogResult.OK)
                {
                    data_loca(sender, e);
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            PublishAdd publishAdd = new PublishAdd();
            publishAdd.ShowDialog();
            if (publishAdd.DialogResult == DialogResult.OK)
            {
                data_loca(sender, e);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            data_loca(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int n = dataGridView1.SelectedRows.Count;//获取当前选中的行
            if (n==0) {
                MessageBox.Show("您未选择任何数据");
            }

            var b= MessageBox.Show("确认删除？", "此删除不可恢复", MessageBoxButtons.YesNo);
            if (b!= DialogResult.Yes) {
                return;
            
            }
            LibraryEntities1 dbContext = new LibraryEntities1();
            for (int i = 0; i < n; i++)
            {
                string publisherId = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                Publisherlnfo pp=dbContext.Publisherlnfo.FirstOrDefault<Publisherlnfo>(P=>P.PublisherId==publisherId);
                dbContext.Publisherlnfo.Remove(pp);

            }
            int result=dbContext.SaveChanges();
            String str = result == 0 ? "删除出版社失败":"删除出版社成功";
            data_loca(sender, e);
            MessageBox.Show(str);
            
        }
    }
}
